CREATE OR REPLACE FUNCTION %TRIGGER_NAME%_func()
RETURNS trigger AS
$$
DECLARE
    changed timestamp;
BEGIN
    changed := now();
        
    IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
        %HISTORY_TRIGGER%
    END IF;

    
    IF TG_OP = 'UPDATE' OR TG_OP = 'INSERT' THEN
        new.%PREFIX%id := nextval('%SEQUENCE_NAME%');
        new.%PREFIX%created := changed;
        RETURN new;
    ELSE
        RETURN old;
    END IF;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;


CREATE TRIGGER %TRIGGER_NAME%
  BEFORE INSERT OR UPDATE OR DELETE
  ON %TABLE_NAME%
  FOR EACH ROW
  EXECUTE PROCEDURE %TRIGGER_NAME%_func();
